package com.bkq.cb;
import java.util.Calendar;

import org.w3c.dom.Text;


import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CustomModeScreen extends Activity {
	private Button back,ok;
	private EditText dayFrom,dayTo;
	TextView tv;
	String idUser,day, nameUser;
	GetDMY temp = new GetDMY();
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.custommode);
	    
	     back = (Button)findViewById(R.id.backIma_CustomMode);
	     ok = (Button)findViewById(R.id.okBnt_CustomMode);
	     dayFrom = (EditText)findViewById(R.id.dayFromTxt_CustomMode);
	     dayTo = (EditText)findViewById(R.id.dayToTxt_CustomMode);
	     tv = (TextView)findViewById(R.id.tv_CustomMode);	     
Bundle extras = getIntent().getExtras();

         if(extras!=null)
	     {
	    idUser = extras.getString("idUser").toString();
	    nameUser = extras.getString("nameUser").toString();
	     }
	     back.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			});
	  
	     
	     dayFrom.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					 int y,m,d;
						Calendar c = Calendar.getInstance();        
			            y = c.get(Calendar.YEAR);        
			              m= c.get(Calendar.MONTH);        
			             d = c.get(Calendar.DAY_OF_MONTH); 
			             DatePickerDialog dp = new DatePickerDialog(CustomModeScreen.this,new DatePickerDialog.OnDateSetListener() {
			            	  

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								// TODO Auto-generated method stub
								  String erg = "";
			                        erg = String.valueOf(dayOfMonth);                      
			                        erg += "/" + String.valueOf(monthOfYear+1);                    
			                        erg += "/" + year;  
			                      dayFrom.setText(erg);
			                      //day = erg;
							}
			            	   
			                 }, y, m, d);
			             
			              dp.setTitle("Choose day:");
			              dp.show();
				}
			});
	     dayTo.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					 int y,m,d;
						Calendar c = Calendar.getInstance();        
			            y = c.get(Calendar.YEAR);        
			              m= c.get(Calendar.MONTH);        
			             d = c.get(Calendar.DAY_OF_MONTH); 
			             DatePickerDialog dp = new DatePickerDialog(CustomModeScreen.this,new DatePickerDialog.OnDateSetListener() {
			            	  

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								// TODO Auto-generated method stub
								  String erg = "";
			                        erg = String.valueOf(dayOfMonth);                      
			                        erg += "/" + String.valueOf(monthOfYear+1);                    
			                        erg += "/" + year;  
			                      dayTo.setText(erg);
			                      //day = erg;
							}
			            	   
			                 }, y, m, d);
			             
			              dp.setTitle("Choose day:");
			              dp.show();
				}
			});
	     ok.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				if(dayFrom.getText().toString().equals(""))
				{
					Toast.makeText(getBaseContext(), "Day From is null", Toast.LENGTH_SHORT).show();
				}
				else if(dayTo.getText().toString().equals(""))
				{
					Toast.makeText(getBaseContext(), "Day To is null", Toast.LENGTH_SHORT).show();
				}
				else
				{
					
					if(Integer.parseInt(temp.getYear(dayFrom.getText().toString()))<Integer.parseInt(temp.getYear(dayTo.getText().toString())))
					{
						Intent i = new Intent(CustomModeScreen.this,CashBookScreen.class);
						i.putExtra("check", "2");
						i.putExtra("idUser", idUser);
						i.putExtra("nameUser", nameUser);
						i.putExtra("day", dayFrom.getText().toString());
						i.putExtra("dayTo", dayTo.getText().toString());
						startActivity(i);
						
					}
					else if(Integer.parseInt(temp.getYear(dayFrom.getText().toString()))>Integer.parseInt(temp.getYear(dayTo.getText().toString())))
					{
						Toast.makeText(getBaseContext(), "Invalid year", Toast.LENGTH_SHORT).show();
						dayTo.setText("");
				
					}
					else
					{
						 if(Integer.parseInt(temp.getMonth(dayFrom.getText().toString()))<Integer.parseInt(temp.getMonth(dayTo.getText().toString())))
							{
							 Intent i = new Intent(CustomModeScreen.this,CashBookScreen.class);
								i.putExtra("check", "2");
								i.putExtra("idUser", idUser);
								i.putExtra("nameUser", nameUser);
								i.putExtra("day", dayFrom.getText().toString());
								i.putExtra("dayTo", dayTo.getText().toString());
								startActivity(i);
							
							}
						 else if(Integer.parseInt(temp.getMonth(dayFrom.getText().toString()))>Integer.parseInt(temp.getMonth(dayTo.getText().toString())))
						 {
							 Toast.makeText(getBaseContext(), "Invalid month", Toast.LENGTH_SHORT).show();
								dayTo.setText("");
						 }
						 else
						 {
							 if(Integer.parseInt(temp.getDay(dayFrom.getText().toString()))>Integer.parseInt(temp.getDay(dayTo.getText().toString())))
								{
									Toast.makeText(getBaseContext(), "Invalid day", Toast.LENGTH_SHORT).show();
									dayTo.setText("");
								}
							 else
							 {
								 Intent i = new Intent(CustomModeScreen.this,CashBookScreen.class);
									i.putExtra("check", "2");
									i.putExtra("idUser", idUser);
									i.putExtra("nameUser", nameUser);
									i.putExtra("day", dayFrom.getText().toString());
									i.putExtra("dayTo", dayTo.getText().toString());
									startActivity(i);
							 }
						 }

					
					}
				
				}
				}
			});
	}
	
}
